package com.zcxy.fast.controller;




import cn.hutool.core.util.CharsetUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.crypto.asymmetric.KeyType;
import cn.hutool.crypto.asymmetric.RSA;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.zcxy.fast.anotation.Log;
import com.zcxy.fast.bean.PageQuery;
import com.zcxy.fast.bean.Response;
import com.zcxy.fast.entity.SysAutocode;
import com.zcxy.fast.entity.SysAutocodeItem;
import com.zcxy.fast.mapper.SysAutocodeMapper;
import com.zcxy.fast.service.ISysAutocodeService;
import com.zcxy.fast.vo.SysAutocodeVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import javax.validation.constraints.NotNull;
import java.io.IOException;
import java.util.List;

/**
 * @author zcc
 * @version 1.0.0
 * @ClassName "SysAutocode"Controller.java
 * @Description "代码生成"
 * @createTime 2020-09-12
 */
@RestController
    @RequestMapping("/api/sysAutocode")
@Api(tags = "代码生成管理")
@Slf4j
public class SysAutocodeController {
    @Autowired
    private ISysAutocodeService iSysAutocodeService;

    @Log("获取代码生成数据--分页")
    @PostMapping(value = "/queryPageList")
    @ApiOperation(value = "获取(代码生成)分页数据列表", notes = "获取(代码生成)分页数据列表")
    public Response<IPage<SysAutocodeVo>> queryPageList(@RequestBody PageQuery<SysAutocode> queryVo) {
        return Response.ok(iSysAutocodeService.queryPageList(queryVo));
    }

    @Log("获取代码生成数据-列表")
    @GetMapping(value = "/queryList")
    @ApiOperation(value = "获取代码生成数据-列表", notes = "获取代码生成数据-列表")
    public Response<List<SysAutocodeVo>> queryList() {
        return Response.ok(iSysAutocodeService.queryList());
    }


    @Log("添加代码生成数据")
    @PostMapping(value = "/add")
    @ApiOperation(value = "添加代码生成数据", notes = "添加代码生成数据/更新和新增一起")
    public Response<SysAutocode> addSysAutocode(@RequestBody @Validated @NotNull SysAutocode queryVo) {

        return Response.ok(iSysAutocodeService.addSysAutocode(queryVo));
    }

    @Log("添加代码生成数据")
    @GetMapping(value = "/view")
    @ApiOperation(value = "添加代码生成数据", notes = "添加代码生成数据/更新和新增一起")
    public Response<SysAutocodeVo> viewSysAutocode(@RequestParam String   id) {
        return Response.ok(iSysAutocodeService.viewSysAutocode(id));
    }



    @Log(value = "批量删除代码生成")
    @ApiOperation("批量删除代码生成")
    @DeleteMapping("/dels")
    public Response deleteByIds(@RequestBody List<String> ids) {
        iSysAutocodeService.removeAllSysAutocode(ids);
        return Response.ok();
    }


    @Log("代码生成")
    @ApiOperation("代码生成")
    @GetMapping(value = "/genCode")
    public void genCode(@RequestParam(name = "codeId")  String codeId, HttpServletResponse httpServletResponse) throws IOException {
            iSysAutocodeService.genCode(codeId,httpServletResponse);
    }



    @Log("获取表名")
    @ApiOperation("获取表名")
    @GetMapping(value = "/getTables")
    public Response<List<SysAutocode>> getTables(@RequestParam(name = "tableName",required = false) String tableName)  {
      return   Response.ok(iSysAutocodeService.getAllTables(tableName));
    }


    @Log("根据表名获取属性")
    @ApiOperation("根据表名获取属性")
    @GetMapping(value = "/getFileds")
    public Response<List<SysAutocodeItem>> getFileds(@RequestParam(name = "tableName",required = false) String tableName)  {
        return   Response.ok(((SysAutocodeMapper)iSysAutocodeService.getBaseMapper()).queryMysqlColums(tableName));
    }


    /*public static void main(String[] args) {
        String privateKey="MIICdgIBADANBgkqhkiG9w0BAQEFAASCAmAwggJcAgEAAoGBALY0BLGSC1vJ7KDYfu2KwNJExdzKazEuljXrFg/otYMap9hf6zioegFWEJYyZmfVmzB3yKcZQjWkNqx6iUT0ZlsqkZ3QWgRghtmNMEwmAh/ouMrwQUJEYDUOWi/HO6gVm68ftiF84ojm3d742Z2z6OPjrMowPJwVe0rawnjtzVZNAgMBAAECgYB6O3+CmNVh6J8EcPkF5vXgmh6bNnie7//5gBK27YkHwRQlBoRI6E2hI9nQyVMGCbko1NMZtspCLj43i6UXS1Z2ZILIa0DP5BT5iWPKvFo4zZzzA264/fK/ibfAVYyZypqv4fLomo+DnactAc2yyelS9+F4pRU5z0lVJKsqwar7CQJBAOLJv1mBKsaIaHi9Tsqn1YOEWHKWZ6gyHI8kUUferPzMq/xeu265x6YwaE65gWcQbZ1VkvQ4m7CHMtUfp3WH3jsCQQDNrBnd9adqnZPrsb7s2x3UWYLB2j+DY3skXnzj4WpuYztJzSo8deGPzZ/KTyGN9EEeSit+IIoEKYgqtomFGC0XAkAVaOxqm7oZEgmi1GRa12tdxiG/d5mDlG+Dx+7wdQIpv3zBD4RsWJeTheBjBjpEK5Xdk5WS/sEb8jeHTTy26MO/AkEAg3PO6JVcloD2jPsC+2GktBctwnaah4BqsTkO+5sUKk+UEjxmFVSGgSG8UlK3MIJ/qDthhvCHJdfhpuMaDA4CqwJAOb2rI7HBjMf4WMsTtTy8LJQbrOzapvhkh0dzXE8hsZmKVkprHwVbfPvNKlYePVdiPOEIhoZotixrObMnnHWPDw==";
        String publicKey="MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQC2NASxkgtbyeyg2H7tisDSRMXcymsxLpY16xYP6LWDGqfYX+s4qHoBVhCWMmZn1Zswd8inGUI1pDaseolE9GZbKpGd0FoEYIbZjTBMJgIf6LjK8EFCRGA1DlovxzuoFZuvH7YhfOKI5t3e+Nmds+jj46zKMDycFXtK2sJ47c1WTQIDAQAB";

        RSA rsa=new RSA(null,publicKey);
        byte[]  a=rsa.encrypt("ddd", KeyType.PublicKey);

        RSA rsa2=new RSA(privateKey,null);
        String b= StrUtil.str(rsa2.decrypt(a,KeyType.PrivateKey), CharsetUtil.CHARSET_UTF_8);

        //
        System.out.println(b);


    }*/
}
